package com.wyz.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import com.wyz.bean.Record;
import com.wyz.entity.User;
import com.wyz.json.Jackson;
import com.wyz.service.ReviewService;
import com.wyz.service.UserService;
import com.wyz.entity.Review;

import my.mvc.IUser;
import my.mvc.RequestContext;
import my.util.ResponseUtils;

public class UserAction {
	
	private UserService userService =new UserService();
	private ReviewService reviewService=new ReviewService();
	private int flag=0;
	private User user=null;
	
	/**
	 * 注册增加用户
	 * */
	public void save(RequestContext rc) throws ServletException, IOException{
		User user=rc.form(User.class);

		userService.save(user);
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
	}
	
	
	public void login(RequestContext rc)  throws ServletException, IOException{

		String inputPhoneNumber=rc.param("phoneNumber", "");
		String inputPassword=rc.param("password", "");
		flag=userService.verify(inputPhoneNumber, inputPassword);
		if(flag==1) {
			
			user=userService.findByPhoneNumber(inputPhoneNumber);
			user.setFlag(flag);
			userService.updateFlag(user, flag);
			rc.saveUserInCookie(user, true);
		}

		String json = Jackson.me().toJson(new Record(true,flag,""));
		ResponseUtils.renderJson(rc.response(), json);
	}
	
	public void logout(RequestContext rc)  throws ServletException, IOException{

			flag=0;
			user.setFlag(flag);
			userService.updateFlag(user, flag);
			rc.deleteUserInCookie();
			user=null;
			
			rc.forward("/pages/index.jsp");
		
	}
	
	
	public void index(RequestContext rc) throws ServletException, IOException{
		IUser iuser=rc.getUserFromCookie();
		
		if(iuser!=null) {
			user=userService.findById(iuser.getId());
			rc.reqAttr("user",user);
			rc.reqAttr("success",true);
		}else {
			rc.reqAttr("success",false);
		}
		
		rc.forward("/pages/index.jsp");
	}
	
	public void index1(RequestContext rc) {
		IUser iuser=rc.getUserFromCookie();
		if(iuser!=null) {
			user=userService.findById(iuser.getId());
			String json = Jackson.me().toJson(user);
			ResponseUtils.renderJson(rc.response(), json);
		}
	}
	
	
	
	
	
    public void update(RequestContext rc) {
		String userName=rc.param("uname", "");
		String email=rc.param("email", "");
		userService.update(user,email,userName);
		user=userService.findById(user.getId());
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
	}
    public void writerInfo(RequestContext rc) throws ServletException, IOException{
    	Long id=rc.param("id", 0L);
    	System.out.println("观察的id为"+id);
    	if(user!=null) {
    		if(user.getId()==id) {
    			rc.reqAttr("user",user);
    			rc.forward("/pages/user_center.jsp");
    			System.out.println("当前用户与作者一致");
    		}else {
    			User writer=userService.findById(id);
    			if(writer!=null) {
    	    		rc.reqAttr("writer",writer);
    	    		rc.reqAttr("user",user);
    	    		rc.forward("/pages/writer_center.jsp");
    	    		System.out.println("当前用户与作者不一致");
    	    	}
    		}
    		
    	}else {
    		User writer=userService.findById(id);
			if(writer!=null) {
	    		rc.reqAttr("writer",writer);
	    		rc.reqAttr("user",user);
	    		rc.forward("/pages/writer_center.jsp");
	    		System.out.println("用户未登录");
	    	}
    	}
    	
    }
    
  
    
    
    
    
    
    
    public void verifyPassword(RequestContext rc)throws ServletException ,IOException{
    	
    	String json = Jackson.me().toJson(user);
		ResponseUtils.renderJson(rc.response(), json);
    }
    
    
    
    public void changePassword(RequestContext rc) throws ServletException ,IOException{
    	String newpasswd=rc.param("newpasswd", "");
    	userService.changePassword(user, newpasswd);
    	user=userService.findById(user.getId());
    	String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
    }
    public void userCenter(RequestContext rc)throws ServletException ,IOException{
    	IUser iuser=rc.getUserFromCookie();
		if(iuser!=null) {
			user=userService.findById(iuser.getId());
			rc.reqAttr("user",user);
			//System.out.println(user.getUserName());
		}
		
		rc.forward("/pages/user_center.jsp");
    }
    
    
    public void listInfo(RequestContext rc) throws ServletException, IOException{
		String json = Jackson.me().toJson(user);
		ResponseUtils.renderJson(rc.response(), json);
	}
	
    
    public void listWriterInfo(RequestContext rc) {
    	Long writerId=rc.param("writerId", 0L);
    	User writer=userService.findById(writerId);
    	if(writer!=null) {
    		String json = Jackson.me().toJson(writer);
    		ResponseUtils.renderJson(rc.response(), json);
    	}
    }
    
}
